home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / fileutil.zip / DD.C < prev    next >
Text File  |  1994-07-16  |  28KB  |  1,046 lines

  1. /* dd -- convert a file while copying it.
  2.    Copyright (C) 1985, 1989, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by Paul Rubin and David MacKenzie. */
  19.  
  20. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  21.    This port is also distributed under the terms of the
  22.    GNU General Public License as published by the
  23.    Free Software Foundation.
  24.  
  25.    Please note that this file is not identical to the
  26.    original GNU release, you should have received this
  27.    code as patch to the official release.
  28.  
  29.     Saturday, 16 July 1994  Troy Rollo (troy@cbme.unsw.EDU.AU)
  30.  
  31.         - Added dummy arg to two calls to usage() to prevent
  32.           BC++ from failing the calls.
  33.  
  34.         - Corrected definition of xmalloc, which did not
  35.           match the earlier declaration
  36.  
  37.         - Made definition of max() no longer conditional
  38.  
  39.  */
  40.  
  41. #ifdef MSDOS
  42. static char RCS_Id[] =
  43.   "$Header: e:/gnu/fileutil/RCS/dd.c 1.4.0.2 90/09/19 11:17:53 tho Exp $";
  44.  
  45. static char Program_Id[] = "dd";
  46. static char RCS_Revision[] = "$Revision: 1.4.0.2 $";
  47.  
  48. #define VERSION \
  49.   "GNU %s, Version %.*s (compiled %s %s for MS-DOS)\n", Program_Id, \
  50.   (sizeof RCS_Revision - 14), (RCS_Revision + 11), __DATE__, __TIME__
  51.  
  52. #define COPYING \
  53.   "This is free software, distributed under the terms of the\n" \
  54.   "GNU General Public License.  For details, see the file COPYING.\n"
  55. #endif /* MSDOS */
  56.  
  57. /* Options:
  58.  
  59.    Numbers can be followed by a multiplier:
  60.    b=512, k=1024, w=2, xm=number m
  61.  
  62.    if=FILE            Read from FILE instead of stdin.
  63.    of=FILE            Write to FILE instead of stdout; don't
  64.                 truncate FILE.
  65.    ibs=BYTES            Read BYTES bytes at a time.
  66.    obs=BYTES            Write BYTES bytes at a time.
  67.    bs=BYTES            Override ibs and obs.
  68.    cbs=BYTES            Convert BYTES bytes at a time.
  69.    skip=BLOCKS            Skip BLOCKS ibs-sized blocks at
  70.                 start of input.
  71.    seek=BLOCKS            Skip BLOCKS obs-sized blocks at
  72.                 start of output.
  73.    count=BLOCKS            Copy only BLOCKS input blocks.
  74.    conv=CONVERSION[,CONVERSION...]
  75.  
  76.    Conversions:
  77.    ascii            Convert EBCDIC to ASCII.
  78.    ebcdic            Convert ASCII to EBCDIC.
  79.    ibm                Convert ASCII to alternate EBCDIC.
  80.    block            Pad newline-terminated records to size of
  81.                 cbs, replacing newline with trailing spaces.
  82.    unblock            Replace trailing spaces in cbs-sized block
  83.                 with newline.
  84.    lcase            Change uppercase characters to lowercase.
  85.    ucase            Change lowercase characters to uppercase.
  86.    swab                Swap every pair of input bytes.
  87.                 Unlike the Unix dd, this works when an odd
  88.                 number of bytes are read.
  89.    noerror            Continue after read errors.
  90.    sync                Pad every input block to size of ibs with
  91.                 trailing NULs. */
  92.  
  93. #ifdef MSDOS
  94. /*
  95.    im={text,binary}        Input file translation mode (default: text)
  96.    om={text,binary}        Output file translation mode (default: text)
  97.  */
  98. #endif
  99.  
  100. #include <stdio.h>
  101. #include <ctype.h>
  102. #ifdef STDC_HEADERS
  103. #define ISLOWER islower
  104. #define ISUPPER isupper
  105. #else
  106. #define ISLOWER(c) (isascii ((c)) && islower ((c)))
  107. #define ISUPPER(c) (isascii ((c)) && isupper ((c)))
  108. #endif
  109. #include <sys/types.h>
  110. #include <signal.h>
  111. #include "system.h"
  112.  
  113. #ifdef STDC_HEADERS
  114. #include <errno.h>
  115. #include <stdlib.h>
  116. #else
  117. char *malloc ();
  118.  
  119. extern int errno;
  120. #endif
  121.  
  122. #ifndef _POSIX_SOURCE
  123. long lseek ();
  124. #endif
  125.  
  126. #define equal(p, q) (strcmp ((p),(q)) == 0)
  127. #define max(a, b) ((a) > (b) ? (a) : (b))
  128.  
  129. /* Default input and output blocksize. */
  130. #define DEFAULT_BLOCKSIZE 512
  131.  
  132. /* Conversions bit masks. */
  133. #define C_ASCII 01
  134. #define C_EBCDIC 02
  135. #define C_IBM 04
  136. #define C_BLOCK 010
  137. #define C_UNBLOCK 020
  138. #define C_LCASE 040
  139. #define C_UCASE 0100
  140. #define C_SWAB 0200
  141. #define C_NOERROR 0400
  142. #define C_SYNC 01000
  143. /* Use separate input and output buffers, and combine partial input blocks. */
  144. #define C_TWOBUFS 04000
  145.  
  146.  
  147. #ifdef MSDOS
  148.  
  149. #include <io.h>
  150. extern void error (int status, int errnum, char *message, ...);
  151.  
  152. extern void main (int argc, char **argv);
  153. static void copy (void);
  154. static void scanargs (int argc, char **argv);
  155. static int parse_integer (char *str);
  156. static void parse_conversion (char *str);
  157. static void apply_translations (void);
  158. static void translate_charset (unsigned char *new_trans);
  159. static int bit_count (unsigned int i);
  160. static void print_stats (void);
  161. static void quit (int code);
  162. static SIGTYPE interrupt_handler (void);
  163. static char *xmalloc (unsigned short n);
  164. static void usage (char *string, char *arg0, char *arg1);
  165.  
  166. #else /* not MSDOS */
  167.  
  168. char *xmalloc ();
  169. SIGTYPE interrupt_handler ();
  170. int bit_count ();
  171. int parse_integer ();
  172. void apply_translations ();
  173. void copy ();
  174. void error ();
  175. void parse_conversion ();
  176. void print_stats ();
  177. void translate_charset ();
  178. void quit ();
  179. void scanargs ();
  180. void usage ();
  181.  
  182. #endif /* not MSDOS */
  183.  
  184. /* The name this program was run with. */
  185. char *program_name;
  186.  
  187. /* The name of the input file, or NULL for the standard input. */
  188. char *input_file = NULL;
  189.  
  190. /* The input file descriptor. */
  191. int input_fd = 0;
  192.  
  193. /* The name of the output file, or NULL for the standard output. */
  194. char *output_file = NULL;
  195.  
  196. /* The output file descriptor. */
  197. int output_fd = 1;
  198.  
  199. /* The number of bytes in which atomic reads are done. */
  200. #ifdef MSDOS
  201. int input_blocksize = -1;
  202. #else
  203. long input_blocksize = -1;
  204. #endif
  205.  
  206. /* The number of bytes in which atomic writes are done. */
  207. #ifdef MSDOS
  208. int output_blocksize = -1;
  209. #else
  210. long output_blocksize = -1;
  211. #endif
  212.  
  213. /* Conversion buffer size, in bytes.  0 prevents conversions. */
  214. #ifdef MSDOS
  215. int conversion_blocksize = 0;
  216. #else
  217. long conversion_blocksize = 0;
  218. #endif
  219.  
  220. /* Skip this many records of `input_blocksize' bytes before input. */
  221. int skip_records = 0;
  222.  
  223. /* Skip this many records of `output_blocksize' bytes before output. */
  224. #ifdef MSDOS
  225. int seek_record = 0;
  226. #else
  227. long seek_record = 0;
  228. #endif
  229.  
  230. /* Copy only this many records.  <0 means no limit. */
  231. int max_record = -1;
  232.  
  233. /* Bit vector of conversions to apply. */
  234. int conversions_mask = 0;
  235.  
  236. /* Number of partial blocks written. */
  237. unsigned w_partial = 0;
  238.  
  239. /* Number of full blocks written. */
  240. unsigned w_full = 0;
  241.  
  242. /* Number of partial blocks read. */
  243. unsigned r_partial = 0;
  244.  
  245. /* Number of full blocks read. */
  246. unsigned r_full = 0;
  247.  
  248. /* Records truncated by conv=block. */
  249. unsigned r_truncate = 0;
  250.  
  251. #ifdef MSDOS
  252. /* Translation modes */
  253. int input_file_mode = O_TEXT;
  254. int output_file_mode = O_TEXT;
  255. #endif
  256.  
  257. /* Output representation of newline and space characters. */
  258. unsigned char newline_character = '\n';
  259. unsigned char space_character = ' ';
  260.  
  261. struct conversion
  262. {
  263.   char *convname;
  264.   int conversion;
  265. };
  266.  
  267. struct conversion conversions[] =
  268. {
  269.   "ascii", C_ASCII | C_TWOBUFS,    /* EBCDIC to ASCII. */
  270.   "ebcdic", C_EBCDIC | C_TWOBUFS,    /* ASCII to EBCDIC. */
  271.   "ibm", C_IBM | C_TWOBUFS,    /* Slightly different ASCII to EBCDIC. */
  272.   "block", C_BLOCK | C_TWOBUFS,    /* Variable to fixed length records. */
  273.   "unblock", C_UNBLOCK | C_TWOBUFS,    /* Fixed to variable length records. */
  274.   "lcase", C_LCASE | C_TWOBUFS,    /* Translate upper to lower case. */
  275.   "ucase", C_UCASE | C_TWOBUFS,    /* Translate lower to upper case. */
  276.   "swab", C_SWAB | C_TWOBUFS,    /* Swap bytes of input. */
  277.   "noerror", C_NOERROR,        /* Ignore i/o errors. */
  278.   "sync", C_SYNC,        /* Pad input records to ibs with NULs. */
  279.   NULL, 0
  280. };
  281.  
  282. /* Translation table formed by applying successive transformations. */
  283. unsigned char trans_table[256];
  284.  
  285. unsigned char ascii_to_ebcdic[] =
  286. {
  287.   0, 01, 02, 03, 067, 055, 056, 057,
  288.   026, 05, 045, 013, 014, 015, 016, 017,
  289.   020, 021, 022, 023, 074, 075, 062, 046,
  290.   030, 031, 077, 047, 034, 035, 036, 037,
  291.   0100, 0117, 0177, 0173, 0133, 0154, 0120, 0175,
  292.   0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
  293.   0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
  294.   0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
  295.   0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
  296.   0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
  297.   0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
  298.   0347, 0350, 0351, 0112, 0340, 0132, 0137, 0155,
  299.   0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
  300.   0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
  301.   0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
  302.   0247, 0250, 0251, 0300, 0152, 0320, 0241, 07,
  303.   040, 041, 042, 043, 044, 025, 06, 027,
  304.   050, 051, 052, 053, 054, 011, 012, 033,
  305.   060, 061, 032, 063, 064, 065, 066, 010,
  306.   070, 071, 072, 073, 04, 024, 076, 0341,
  307.   0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
  308.   0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  309.   0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
  310.   0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
  311.   0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
  312.   0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236,
  313.   0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257,
  314.   0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
  315.   0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
  316.   0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
  317.   0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
  318.   0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377
  319. };
  320.  
  321. unsigned char ascii_to_ibm[] =
  322. {
  323.   0, 01, 02, 03, 067, 055, 056, 057,
  324.   026, 05, 045, 013, 014, 015, 016, 017,
  325.   020, 021, 022, 023, 074, 075, 062, 046,
  326.   030, 031, 077, 047, 034, 035, 036, 037,
  327.   0100, 0132, 0177, 0173, 0133, 0154, 0120, 0175,
  328.   0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
  329.   0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
  330.   0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
  331.   0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
  332.   0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
  333.   0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
  334.   0347, 0350, 0351, 0255, 0340, 0275, 0137, 0155,
  335.   0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
  336.   0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
  337.   0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
  338.   0247, 0250, 0251, 0300, 0117, 0320, 0241, 07,
  339.   040, 041, 042, 043, 044, 025, 06, 027,
  340.   050, 051, 052, 053, 054, 011, 012, 033,
  341.   060, 061, 032, 063, 064, 065, 066, 010,
  342.   070, 071, 072, 073, 04, 024, 076, 0341,
  343.   0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
  344.   0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
  345.   0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
  346.   0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
  347.   0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
  348.   0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236,
  349.   0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257,
  350.   0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
  351.   0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
  352.   0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
  353.   0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
  354.   0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377
  355. };
  356.  
  357. unsigned char ebcdic_to_ascii[] =
  358. {
  359.   0, 01, 02, 03, 0234, 011, 0206, 0177,
  360.   0227, 0215, 0216, 013, 014, 015, 016, 017,
  361.   020, 021, 022, 023, 0235, 0205, 010, 0207,
  362.   030, 031, 0222, 0217, 034, 035, 036, 037,
  363.   0200, 0201, 0202, 0203, 0204, 012, 027, 033,
  364.   0210, 0211, 0212, 0213, 0214, 05, 06, 07,
  365.   0220, 0221, 026, 0223, 0224, 0225, 0226, 04,
  366.   0230, 0231, 0232, 0233, 024, 025, 0236, 032,
  367.   040, 0240, 0241, 0242, 0243, 0244, 0245, 0246,
  368.   0247, 0250, 0133, 056, 074, 050, 053, 041,
  369.   046, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
  370.   0260, 0261, 0135, 044, 052, 051, 073, 0136,
  371.   055, 057, 0262, 0263, 0264, 0265, 0266, 0267,
  372.   0270, 0271, 0174, 054, 045, 0137, 076, 077,
  373.   0272, 0273, 0274, 0275, 0276, 0277, 0300, 0301,
  374.   0302, 0140, 072, 043, 0100, 047, 075, 042,
  375.   0303, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
  376.   0150, 0151, 0304, 0305, 0306, 0307, 0310, 0311,
  377.   0312, 0152, 0153, 0154, 0155, 0156, 0157, 0160,
  378.   0161, 0162, 0313, 0314, 0315, 0316, 0317, 0320,
  379.   0321, 0176, 0163, 0164, 0165, 0166, 0167, 0170,
  380.   0171, 0172, 0322, 0323, 0324, 0325, 0326, 0327,
  381.   0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
  382.   0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
  383.   0173, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
  384.   0110, 0111, 0350, 0351, 0352, 0353, 0354, 0355,
  385.   0175, 0112, 0113, 0114, 0115, 0116, 0117, 0120,
  386.   0121, 0122, 0356, 0357, 0360, 0361, 0362, 0363,
  387.   0134, 0237, 0123, 0124, 0125, 0126, 0127, 0130,
  388.   0131, 0132, 0364, 0365, 0366, 0367, 0370, 0371,
  389.   060, 061, 062, 063, 064, 065, 066, 067,
  390.   070, 071, 0372, 0373, 0374, 0375, 0376, 0377
  391. };
  392.  
  393. void
  394. main (argc, argv)
  395.      int argc;
  396.      char **argv;
  397. {
  398.   int i;
  399.  
  400.   program_name = argv[0];
  401.  
  402.   /* Initialize translation table to identity translation. */
  403.   for (i = 0; i < 256; i++)
  404.     trans_table[i] = i;
  405.  
  406.  
  407.   /* Decode arguments. */
  408.   scanargs (argc, argv);
  409.   apply_translations ();
  410.  
  411.   if (input_file != NULL)
  412.     {
  413.       input_fd = open (input_file, O_RDONLY);
  414.       if (input_fd < 0)
  415.     error (1, errno, "%s", input_file);
  416.     }
  417.   else
  418.     input_file = "standard input";
  419.  
  420.   if (output_file != NULL)
  421.     {
  422.       output_fd = open (output_file, O_RDWR | O_CREAT, 0666);
  423.       if (output_fd < 0)
  424.     error (1, errno, "%s", output_file);
  425.     }
  426.   else
  427.     output_file = "standard output";
  428.  
  429. #ifdef MSDOS
  430.   setmode (input_fd, input_file_mode);
  431.   setmode (output_fd, output_file_mode);
  432. #endif
  433.  
  434.   signal (SIGINT, interrupt_handler);
  435.   copy ();
  436. }
  437.  
  438. void
  439. copy ()
  440. {
  441.   struct stat in_stats, out_stats;
  442.   register unsigned char c;    /* Each char being converted. */
  443.   register unsigned char savec, tempc; /* Copies of `c'. */
  444.   long in_offset = 0L;        /* Offset into input file for conv=swab. */
  445.   register int i;        /* Index into `ibuf'. */
  446.   int oc = 0;            /* Index into `obuf'. */
  447.   int pending_spaces = 0;    /* Number of pending blanks to add. */
  448.   int col = 0;            /* Index into each line. */
  449.   unsigned char *ibuf, *obuf;    /* Buffers. */
  450.   int nread, nwritten;        /* Byte counts for each block. */
  451.   int exit_status = 0;
  452.  
  453.   ibuf = (unsigned char *) xmalloc (input_blocksize);
  454.   if (conversions_mask & C_TWOBUFS)
  455.     obuf = (unsigned char *) xmalloc (output_blocksize);
  456.   else
  457.     obuf = ibuf;
  458.  
  459.   /* Use fstat instead of checking for errno == ESPIPE because
  460.      lseek doesn't work on some special files but doesn't return an
  461.      error, either. */
  462.   if (fstat (input_fd, &in_stats))
  463.     {
  464.       error (0, errno, "%s", input_file);
  465.       quit (1);
  466.     }
  467.   if ((in_stats.st_mode & S_IFMT) == S_IFREG)
  468.     {
  469. #ifdef MSDOS
  470.       if (lseek (input_fd, skip_records * (long) input_blocksize, L_SET) < 0)
  471. #else
  472.       if (lseek (input_fd, skip_records * input_blocksize, L_SET) < 0)
  473. #endif
  474.     {
  475.       error (0, errno, "%s", input_file);
  476.       quit (1);
  477.     }
  478.     }
  479.   else
  480.     {
  481.       for (i = 0; i < skip_records; i++)
  482.     if (read (input_fd, ibuf, input_blocksize) < 0)
  483.       {
  484.         error (0, errno, "%s", input_file);
  485.         quit (1);
  486.       }
  487.       /* What if fewer bytes were read than requested? */
  488.     }
  489.  
  490.   if (fstat (output_fd, &out_stats))
  491.     {
  492.       error (0, errno, "%s", output_file);
  493.       quit (1);
  494.     }
  495.   if ((out_stats.st_mode & S_IFMT) == S_IFREG)
  496.     {
  497. #ifdef MSDOS
  498.       if (lseek (output_fd, seek_record * (long) output_blocksize, L_SET) < 0)
  499. #else
  500.       if (lseek (output_fd, seek_record * output_blocksize, L_SET) < 0)
  501. #endif
  502.     {
  503.       error (0, errno, "%s", output_file);
  504.       quit (1);
  505.     }
  506.     }
  507.   else
  508.     {
  509.       for (i = 0; i < seek_record; i++)
  510.     if (read (output_fd, obuf, output_blocksize) < 0)
  511.       {
  512.         error (0, errno, "%s", output_file);
  513.         quit (1);
  514.       }
  515.       /* What if fewer bytes were read than requested? */
  516.     }
  517.   
  518.   if (max_record >= 0 && r_partial + r_full >= max_record)
  519.     return;
  520.  
  521.   while (1)
  522.     {
  523.       if (max_record >= 0 && r_partial + r_full >= max_record)
  524.     break;
  525.       if ((conversions_mask & C_SYNC) && (conversions_mask & C_NOERROR))
  526.     bzero (ibuf, input_blocksize);
  527.       nread = read (input_fd, ibuf, input_blocksize);
  528.       if (nread == 0)
  529.     break;
  530.       if (nread < 0)
  531.     {
  532.       error (0, errno, "%s", input_file);
  533.       if (conversions_mask & C_NOERROR)
  534.         {
  535.           print_stats ();
  536.           /* Seek past the bad block if possible. */
  537.           lseek (input_fd, input_blocksize, L_INCR);
  538.           if (conversions_mask & C_SYNC)
  539.         nread = 0;
  540.           else
  541.         continue;
  542.         }
  543.       else
  544.         {
  545.           /* Write any partial block. */
  546.           exit_status = 2;
  547.           break;
  548.         }
  549.     }
  550.  
  551.       if (nread < input_blocksize)
  552.     {
  553.       r_partial++;
  554.       if (conversions_mask & C_SYNC)
  555.         {
  556.           if (conversions_mask & C_NOERROR)
  557.         nread = input_blocksize;
  558.           else
  559.         while (nread < input_blocksize)
  560.           ibuf[nread++] = '\0';
  561.         }
  562.     }
  563.       else
  564.     r_full++;
  565.  
  566.       if (ibuf == obuf)        /* If not C_TWOBUFS. */
  567.     {
  568.       nwritten = write (output_fd, obuf, nread);
  569.       if (nwritten != nread)
  570.         {
  571.           error (0, errno, "%s", output_file);
  572.           if (nwritten > 0)
  573.         w_partial++;
  574.           quit (1);
  575.         }
  576.       else if (nread == input_blocksize)
  577.         w_full++;
  578.       else
  579.         w_partial++;
  580.       continue;
  581.     }
  582.  
  583.       /* Copy to output, doing conversion and flushing output as necessary. */
  584.       for (i = 0; i < nread; i++)
  585.     {
  586.       c = trans_table[ibuf[i]];
  587.  
  588.       if (conversions_mask & C_SWAB)
  589.         {
  590.           if (++in_offset & 1)
  591.         {
  592.           /* Swap `c' and `savec'. */
  593.           tempc = c;
  594.           c = savec;
  595.           savec = tempc;
  596.           if (in_offset == 1)
  597.             continue;    /* No previous character to process. */
  598.         }
  599.         }
  600.  
  601.       if (conversions_mask & C_BLOCK) /* Pad with spaces. */
  602.         {
  603.           if (pending_spaces > 0)
  604.         {
  605.           pending_spaces--;
  606.           i--;        /* Push char back; get it later. */
  607.           c = space_character;
  608.         }
  609.           else if (c == newline_character)
  610.         {
  611.           pending_spaces = max (0, conversion_blocksize - col);
  612.           col = 0;
  613.           continue;
  614.         }
  615.           else if (col++ == conversion_blocksize)
  616.         {
  617.           r_truncate++;
  618.           continue;
  619.         }
  620.           else if (col - 1 > conversion_blocksize)
  621.         {
  622.           continue;
  623.         }
  624.         }
  625.       else if (conversions_mask & C_UNBLOCK) /* Strip trailing spaces. */
  626.         {
  627.           if (col++ >= conversion_blocksize)
  628.         {
  629.           col = pending_spaces = 0;
  630.           i--;        /* Push char back; get it later. */
  631.           c = newline_character;
  632.         }
  633.           else if (c == space_character)
  634.         {
  635.           pending_spaces++;
  636.           continue;
  637.         }
  638.           else if (pending_spaces > 0)
  639.         {
  640.           /* Hit the end of a run of spaces that were not at the
  641.              end of the conversion buffer.  Add them to the output. */
  642.           pending_spaces--;
  643.           col--;
  644.           i--;        /* Push char back; get it later. */
  645.           c = space_character;
  646.         }
  647.         }
  648.  
  649.       obuf[oc++] = c;
  650.       if (oc >= output_blocksize)
  651.         {
  652.           nwritten = write (output_fd, obuf, output_blocksize);
  653.           if (nwritten != output_blocksize)
  654.         {
  655.           error (0, errno, "%s", output_file);
  656.           if (nwritten > 0)
  657.             w_partial++;
  658.           quit (1);
  659.         }
  660.           else
  661.         w_full++;
  662.           oc = 0;
  663.         }
  664.     }
  665.     }
  666.  
  667.   if ((conversions_mask & C_SWAB) && in_offset > 0L)
  668.     {
  669.       obuf[oc++] = savec;
  670.       if (oc >= output_blocksize)
  671.     {
  672.       nwritten = write (output_fd, obuf, output_blocksize);
  673.       if (nwritten != output_blocksize)
  674.         {
  675.           error (0, errno, "%s", output_file);
  676.           if (nwritten > 0)
  677.         w_partial++;
  678.           quit (1);
  679.         }
  680.       else
  681.         w_full++;
  682.       oc = 0;
  683.     }
  684.     }
  685.  
  686.   if (conversions_mask & C_BLOCK)
  687.     {
  688.       /* The last record might need to be padded. */
  689.       while (pending_spaces-- > 0)
  690.     {
  691.       obuf[oc++] = space_character;
  692.       if (oc >= output_blocksize)
  693.         {
  694.           nwritten = write (output_fd, obuf, output_blocksize);
  695.           if (nwritten != output_blocksize)
  696.         {
  697.           error (0, errno, "%s", output_file);
  698.           if (nwritten > 0)
  699.             w_partial++;
  700.           quit (1);
  701.         }
  702.           else
  703.         w_full++;
  704.           oc = 0;
  705.         }
  706.     }
  707.     }
  708.   else if (conversions_mask & C_UNBLOCK)
  709.     {
  710.       /* Add a final '\n' if there are exactly `conversion_blocksize'
  711.      characters in the final record. */
  712.       if (col == conversion_blocksize)
  713.     obuf[oc++] = newline_character;
  714.     }
  715.  
  716.   /* Flush last block. */
  717.   if (oc > 0)
  718.     {
  719.       nwritten = write (output_fd, obuf, oc);
  720.       if (nwritten > 0)
  721.     w_partial++;
  722.       if (nwritten != oc)
  723.     {
  724.       error (0, errno, "%s", output_file);
  725.       quit (1);
  726.     }
  727.     }
  728.  
  729. #if 0
  730.   /* 1003.2 draft 10 behavior that is perhaps not justified. */
  731.   if (seek_record > 0 && (out_stats.st_mode & S_IFMT) == S_IFREG
  732.       && ftruncate (output_fd, lseek (output_fd, 0, L_XTND)) < 0)
  733.     error (0, errno, "%s", output_file);
  734. #endif
  735.  
  736.   quit (exit_status);
  737. }
  738.  
  739. void
  740. scanargs (argc, argv)
  741.      int argc;
  742.      char **argv;
  743. {
  744.   int i, n;
  745.  
  746.   for (i = 1; i < argc; i++)
  747.     {
  748.       char *name, *val;
  749.  
  750.       name = argv[i];
  751. #ifdef MSDOS
  752.       if (equal (name, "copying"))
  753.     {
  754.       fprintf (stderr, COPYING);
  755.       exit (0);
  756.     }
  757.       else if (equal (name, "version"))
  758.     {
  759.       fprintf (stderr, VERSION);
  760.       exit (0);
  761.     }
  762. #endif
  763.       val = index (name, '=');
  764.       if (val == NULL)
  765.     usage ("unrecognized option `%s'", name, 0);
  766.       *val++ = '\0';
  767.  
  768.       if (equal (name, "if"))
  769.     input_file = val;
  770.       else if (equal (name, "of"))
  771.     output_file = val;
  772.       else if (equal (name, "conv"))
  773.     parse_conversion (val);
  774. #ifdef MSDOS
  775.       else if (equal (name, "im"))
  776.     {
  777.       if (equal (val, "text"))
  778.         input_file_mode = O_TEXT;
  779.       else if (equal (val, "binary"))
  780.         input_file_mode = O_BINARY;
  781.       else
  782.         error (1, 0, "Input file mode must be text or binary");
  783.     }
  784.       else if (equal (name, "om"))
  785.     {
  786.       if (equal (val, "text"))
  787.         output_file_mode = O_TEXT;
  788.       else if (equal (val, "binary"))
  789.         output_file_mode = O_BINARY;
  790.       else
  791.         error (1, 0, "Output file mode must be text or binary");
  792.     }
  793. #endif /* MSDOS */
  794.       else
  795.     {
  796.       n = parse_integer (val);
  797.       if (n < 0)
  798.         error (1, 0, "invalid number `%s'", val);
  799.  
  800.       if (equal (name, "ibs"))
  801.         {
  802.           input_blocksize = n;
  803.           conversions_mask |= C_TWOBUFS;
  804.         }
  805.       else if (equal (name, "obs"))
  806.         {
  807.           output_blocksize = n;
  808.           conversions_mask |= C_TWOBUFS;
  809.         }
  810.       else if (equal (name, "bs"))
  811.         output_blocksize = input_blocksize = n;
  812.       else if (equal (name, "cbs"))
  813.         conversion_blocksize = n;
  814.       else if (equal (name, "skip"))
  815.         skip_records = n;
  816.       else if (equal (name, "seek"))
  817.         seek_record = n;
  818.       else if (equal (name, "count"))
  819.         max_record = n;
  820.       else
  821.         usage ("unrecognized option `%s=%s'", name, val);
  822.     }
  823.     }
  824.  
  825.   /* If bs= was given, both `input_blocksize' and `output_blocksize' will
  826.      have been set to non-negative values.  If either has not been set,
  827.      bs= was not given, so make sure two buffers are used. */
  828.   if (input_blocksize == -1 || output_blocksize == -1)
  829.     conversions_mask |= C_TWOBUFS;
  830.   if (input_blocksize == -1)
  831.     input_blocksize = DEFAULT_BLOCKSIZE;
  832.   if (output_blocksize == -1)
  833.     output_blocksize = DEFAULT_BLOCKSIZE;
  834.   if (conversion_blocksize == 0)
  835.     conversions_mask &= ~(C_BLOCK | C_UNBLOCK);
  836. }
  837.  
  838. /* Return the value of STR, interpreted as a non-negative decimal integer,
  839.    optionally multiplied by various values.
  840.    Return -1 if STR does not represent a number in this format. */
  841.  
  842. int
  843. parse_integer (str)
  844.      char *str;
  845. {
  846.   register int n = 0;
  847.   register int temp;
  848.   register char *p = str;
  849.  
  850.   while (isdigit (*p))
  851.     {
  852.       n = n * 10 + *p - '0';
  853.       p++;
  854.     }
  855. loop:
  856.   switch (*p++)
  857.     {
  858.     case '\0':
  859.       return n;
  860.     case 'b':
  861.       n *= 512;
  862.       goto loop;
  863.     case 'k':
  864.       n *= 1024;
  865.       goto loop;
  866.     case 'w':
  867.       n *= 2;
  868.       goto loop;
  869.     case 'x':
  870.       temp = parse_integer (p);
  871.       if (temp == -1)
  872.     return -1;
  873.       n *= temp;
  874.       break;
  875.     default:
  876.       return -1;
  877.     }
  878.   return n;
  879. }
  880.  
  881. /* Interpret one "conv=..." option. */
  882.  
  883. void
  884. parse_conversion (str)
  885.      char *str;
  886. {
  887.   char *new;
  888.   int i;
  889.  
  890.   do
  891.     {
  892.       new = index (str, ',');
  893.       if (new != NULL)
  894.     *new++ = '\0';
  895.       for (i = 0; conversions[i].convname != NULL; i++)
  896.     if (equal (conversions[i].convname, str))
  897.       {
  898.         conversions_mask |= conversions[i].conversion;
  899.         break;
  900.       }
  901.       if (conversions[i].convname == NULL)
  902.     {
  903.       usage ("%s: invalid conversion", str, 0);
  904.       exit (1);
  905.     }
  906.       str = new;
  907.   } while (new != NULL);
  908. }
  909.  
  910. /* Fix up translation table. */
  911.  
  912. void
  913. apply_translations ()
  914. {
  915.   int i;
  916.  
  917. #define MX(a) (bit_count (conversions_mask & (a)))
  918.   if ((MX (C_ASCII | C_EBCDIC | C_IBM) > 1)
  919.       || (MX (C_BLOCK | C_UNBLOCK) > 1)
  920.       || (MX (C_LCASE | C_UCASE) > 1)
  921.       || (MX (C_UNBLOCK | C_SYNC) > 1))
  922.     {
  923.       error (1, 0, "\
  924. only one conv in {ascii,ebcdic,ibm}, {lcase,ucase}, {block,unblock}, {unblock,sync}");
  925.     }
  926. #undef MX
  927.  
  928.   if (conversions_mask & C_ASCII)
  929.     translate_charset (ebcdic_to_ascii);
  930.  
  931.   if (conversions_mask & C_UCASE)
  932.     {
  933.       for (i = 0; i < 256; i++)
  934.     if (ISLOWER (trans_table[i]))
  935.       trans_table[i] = toupper (trans_table[i]);
  936.     }
  937.   else if (conversions_mask & C_LCASE)
  938.     {
  939.       for (i = 0; i < 256; i++)
  940.     if (ISUPPER (trans_table[i]))
  941.       trans_table[i] = tolower (trans_table[i]);
  942.     }
  943.  
  944.   if (conversions_mask & C_EBCDIC)
  945.     {
  946.       translate_charset (ascii_to_ebcdic);
  947.       newline_character = ascii_to_ebcdic['\n'];
  948.       space_character = ascii_to_ebcdic[' '];
  949.     }
  950.   else if (conversions_mask & C_IBM)
  951.     {
  952.       translate_charset (ascii_to_ibm);
  953.       newline_character = ascii_to_ibm['\n'];
  954.       space_character = ascii_to_ibm[' '];
  955.     }
  956. }
  957.  
  958. void
  959. translate_charset (new_trans)
  960.      unsigned char *new_trans;
  961. {
  962.   int i;
  963.  
  964.   for (i = 0; i < 256; i++)
  965.     trans_table[i] = new_trans[trans_table[i]];
  966. }
  967.  
  968. /* Return the number of 1 bits in `i'. */
  969.  
  970. int
  971. bit_count (i)
  972.      register unsigned int i;
  973. {
  974.   register int set_bits;
  975.  
  976.   for (set_bits = 0; i != 0; set_bits++)
  977.     i &= i - 1;
  978.   return set_bits;
  979. }
  980.  
  981. void
  982. print_stats ()
  983. {
  984.   fprintf (stderr, "%u+%u records in\n", r_full, r_partial);
  985.   fprintf (stderr, "%u+%u records out\n", w_full, w_partial);
  986.   if (r_truncate > 0)
  987.     fprintf (stderr, "%u truncated record%s\n", r_truncate,
  988.          r_truncate == 1 ? "" : "s");
  989. }
  990.  
  991. void
  992. quit (code)
  993.      int code;
  994. {
  995.   print_stats ();
  996.   exit (code);
  997. }
  998.  
  999. SIGTYPE
  1000. interrupt_handler ()
  1001. {
  1002.   quit (1);
  1003. }
  1004.  
  1005. /* malloc or die. */
  1006.  
  1007. char *
  1008. xmalloc (n)
  1009.      unsigned short n;
  1010. {
  1011.   char *p;
  1012.  
  1013.   p = malloc (n);
  1014.   if (p == NULL)
  1015.     error (3, 0, "virtual memory exhausted");
  1016.   return p;
  1017. }
  1018.  
  1019. void
  1020. usage (string, arg0, arg1)
  1021.      char *string, *arg0, *arg1;
  1022. {
  1023.   fprintf (stderr, "%s: ", program_name);
  1024.   fprintf (stderr, string, arg0, arg1);
  1025.   fprintf (stderr, "\n");
  1026. #ifdef MSDOS
  1027.   fprintf (stderr, "\
  1028. Usage: %s [if=file] [of=file] [ibs=bytes] [obs=bytes] [bs=bytes] [cbs=bytes]\n\
  1029.        [skip=blocks] [seek=blocks] [count=blocks]\n\
  1030.        [im={text,binary}] [om={text,binary}] [copying] [version]\n\
  1031.        [conv={ascii,ebcdic,ibm,block,unblock,lcase,ucase,swab,noerror,sync}]\n\
  1032. Numbers can be followed by a multiplier:\n\
  1033. b=512, k=1024, w=2, xm=number m\n",
  1034.        program_name);
  1035. #else /* not MSDOS */
  1036.   fprintf (stderr, "\
  1037. Usage: %s [if=file] [of=file] [ibs=bytes] [obs=bytes] [bs=bytes] [cbs=bytes]\n\
  1038.        [skip=blocks] [seek=blocks] [count=blocks]\n\
  1039.        [conv={ascii,ebcdic,ibm,block,unblock,lcase,ucase,swab,noerror,sync}]\n\
  1040. Numbers can be followed by a multiplier:\n\
  1041. b=512, k=1024, w=2, xm=number m\n",
  1042.        program_name);
  1043. #endif /* not MSDOS */
  1044.   exit (1);
  1045. }
  1046.